
<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true">
        <el-form-item label="用户名称" prop="userName">
           <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
           />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
           <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
           />
        </el-form-item>
        <el-form-item>
           <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
           <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
     </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          v-hasPermi="['system:business:add']"
          @click="handleAdd"
          >添加联系人</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="CircleClose"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:business:delete']"
          >批量删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Close" @click="handleClose"
          >关闭</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    
    <el-table
      v-loading="loading"
      :data="contactsList"
      @selection-change="handleSelectionChange"
        :row-class-name="rowClassName"
    >
      <el-table-column
        type="selection"
        :selectable="selectable"
        width="55"
        align="center"
      />
      <el-table-column
      
        label="联系人名称"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="联系人職稱"
        prop="contactPosition"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="联系人电话"
        prop="contactNumber"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="e-mail"
        prop="contactAddress"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      
        label="在职状态"
        prop="isTerminated"
        :show-overflow-tooltip="true"
      >
      <template #default="scope">
  <el-tag
    :type="scope.row.isTerminated === 1 ? 'success' : 'danger'"
    size="small"
  >
    {{ scope.row.isTerminated === 1 ? "在职" : "离职" }}
  </el-tag>
</template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:business:update']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除(默认联系人无法删除)" placement="top">
            <el-button
              link
              v-if="scope.row.first !== 1"
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:business:delete']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="addclientele"
        :model="form"
        label-width="100px"
        :rules="rules"
      >
        <el-form-item label="联系人名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入联系名称" />
        </el-form-item>
        <el-form-item label="联系人職稱:" prop="contactPosition">
          <el-input
            v-model="form.contactPosition"
            placeholder="请输入联系人職稱"
          />
        </el-form-item>
        <el-form-item label="联系人电话:" prop="contactNumber">
          <el-input v-model="form.contactNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="e-mail:" prop="contactAddress">
          <el-input
            v-model="form.contactAddress"
            placeholder="请输入联系e-mail"
          />
        </el-form-item>
        <el-form-item label="主联系人:" prop="first">
          <el-switch
            v-model="form.first"
            :active-value="1"
            :inactive-value="2"
            active-text="是"
            inactive-text="否"
          />
        </el-form-item>
        {{ edit_1 }}
        <!-- 新增是否离职选项 -->
        <el-form-item
          label="是否离职:"
          prop="dimission"
          v-if="form.id !== null"
        >
          <el-switch
            v-model="form.dimission"
            active-text="在职"
            inactive-text="离职"
          />
        </el-form-item>
        {{ edit_2 }}
        <!-- 结束 -->
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Auth">
import {
  listcontacts,
  delcontacts,
  addcontacts,
  updatecontacts,
} from "@/api/business/contacts.js";
import { ref } from "vue";
import draggable from 'vuedraggable'; // 引入拖拽组件


const route = useRoute();
const { proxy } = getCurrentInstance();

const ids = ref([]);
const contactsList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const open = ref(false);
const total = ref(0);
const userIds = ref([]);
const title = ref("");
const form = ref({});

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  businessId: route.params.Id,

  rules: {
    name: [{ required: true, message: "联系人名称不能为空", trigger: "blur" }],
  },
});


const rowClassName = (row) => {
  return row.isTerminated !== 1 ? 'text-gray' : '';
};
const { rules } = toRefs(queryParams);
function reset() {
  form.value.name = "";
  form.value.contactNumber = "";
  form.value.contactAddress = "";
  form.value.businessId = contactsList.value[0]?.businessId || null;
  form.value.id = null;
}

//添加联系人按钮
function submitForm() {
  proxy.$refs["addclientele"].validate((valid) => {
    if (valid) {
      // 合并数据
       // 合并数据
       const mergedData = { 
        ...form.value,
      };

      // 在修改联系人时检查 dimission 的值
      if (form.value.id != null) {
        if (form.value.dimission === false) {
          mergedData.isTerminated = 2; // 设置 isTerminated 为 2
        }else if (form.value.dimission === true) {
          mergedData.isTerminated = 1; // 设置 isTerminated 为 1
        }
        updatecontacts(mergedData).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addcontacts(mergedData).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 添加联系人 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加联系人";
}
/** 查询授权用户列表 */
function getList() {
  loading.value = true;
  listcontacts(queryParams).then((response) => {
    contactsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 返回按钮
function handleClose() {
  const obj = { path: "/business/business/add" };
  proxy.$tab.closeOpenPage(obj);
}

/** 修改角色 */
function handleUpdate(row) {
  form.value.name = row.name;
  form.value.businessId = row.businessId;
  form.value.id = row.id;
  form.value.firstname = row.name;
  form.value.contactNumber = row.contactNumber;
  form.value.contactAddress = row.contactAddress;
  form.value.isTerminated = row.isTerminated;
  form.value.first = row.first;

  // 根据 isTerminated 的值设置按钮显示文本
  if (row.isTerminated === 1) {
    form.value.dimission = true; // 设置在职文本
  } else {
    form.value.dimission = false; // 设置离职文本
  }

  open.value = true;
  title.value = "修改联系人";
}

//禁止选择first默认联系人
const selectable = (row) => ![1].includes(row.first);
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  multiple.value = !selection.length;
}

/** 取消按钮 */
function cancel() {
  reset();
  open.value = false;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除为"' + id + '"的数据项?')
    .then(function () {
      return delcontacts(id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>

<style scoped>
.text-gray {
  color: rgb(255, 0, 0); /* 或者您想要的任何灰色 */
}
</style>